# coding:utf-8
import matplotlib
# 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends中 FigureCanvas继承自QtWidgets.QWidget)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout
import matplotlib.pyplot as plt
import numpy as np
import sys
"""学好pyplot API和面向对象 API搞定matplotlib绘图显示在GUI界面上"""
 
class Main_window(QDialog):
    def __init__(self):
        super().__init__()
        # 三步走，定Figure,定Axes,定FigureCanvas
        # 1 直接一段代码搞定figure和axes
        self.figure, (self.ax1, self.ax2) = plt.subplots(figsize=(13, 3), ncols=2)
 
        # 2 先创建figure再创建axes
        # 2.1 用plt.figure()  /  Figure() 创建figure, 推荐前者
        self.figure = plt.figure(figsize=(5,3), facecolor='#FFD7C4')
        # self.figure = Figure(figsize=(5,3), facecolor='#FFD7C4')
        # 2.2 用plt.subplots()  /  plt.add_subplot() 创建axes， 推荐前者
        (self.ax1, self.ax2) = self.figure.subplots(1, 2)
        # ax1 = self.figure.add_subplot(121)
        # ax2 = self.figure.add_subplot(122)
 
        # 3 绑定figure到canvas上
        self.canvas = FigureCanvas(self.figure)
 
        self.button_draw = QPushButton("绘图")
        self.button_draw.clicked.connect(self.Draw)
 
        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(self.button_draw)
        self.setLayout(layout)
 
    def Draw(self):
        AgeList = ['10', '21', '12', '14', '25']
        NameList = ['Tom', 'Jon', 'Alice', 'Mike', 'Mary']
        # 将AgeList中的数据转化为int类型
        AgeList = list(map(int, AgeList))
 
        # 将x,y转化为numpy数据类型，对于matplotlib很重要
        self.x = np.arange(len(NameList)) + 1
        self.y = np.array(AgeList)
 
        # tick_label后边跟x轴上的值，（可选选项：color后面跟柱型的颜色，width后边跟柱体的宽度）
        self.ax1.bar(range(len(NameList)), AgeList, tick_label=NameList, color='green', width=0.5)
        for a, b in zip(self.x, self.y):
            self.ax1.text(a-1, b, '%d' % b, ha='center', va='bottom')
        plt.title("Demo")
 
        pos = self.ax2.imshow(np.random.random((100, 100)), cmap=plt.cm.BuPu_r)
        self.figure.colorbar(pos, ax=self.ax2)      # 终于可以用colorbar了
 
        self.canvas.draw()
 
 
# 运行程序
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main_window = Main_window()
    main_window.show()
    app.exec()
# ————————————————
# 版权声明：本文为CSDN博主「粉刷工匠」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
# 原文链接：https://blog.csdn.net/weixin_42053726/article/details/97650099


## 没有找到合适的在linux下实现窗体的嵌套的方法，因此选择直接裸奔
## 主要还是原来的想法
## 1. 登陆界面 ok
## 2. 操作与简单仿真界面 ok
## 3. 训练界面 ok了
## 4. 路线显示界面 显示寻路地图


import matplotlib as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5 import QtWidgets, QtCore
import sys

plt.use('Qt5Agg')


class MyFigureCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)

        self.compute_initial_figure()

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QtWidgets.QSizePolicy.Expanding,
                                   QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        y = (3, 2, 5, 6, 4)
        x = (1, 2, 3, 4, 5)
        self.axes.plot(x, y, 'ro--')


class MainWindow2(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.main_widget = QtWidgets.QWidget(self)
        ll = QtWidgets.QVBoxLayout(self.main_widget)
        mc = MyFigureCanvas(self.main_widget, width=5, height=4, dpi=100)
        ll.addWidget(mc)
        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

    def fileQuit(self):
        self.close()

    def closeEvent(self, ce):
        self.fileQuit()


if __name__ == '__main__':
    qApp = QtWidgets.QApplication(sys.argv)
    ui = MainWindow2()
    ui.setWindowTitle("Test")
    ui.show()
    sys.exit(qApp.exec_())
